package com.xuecheng.content.api;

import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Tang
 * @version 1.0
 * @date 2024/1/6 10:30
 */
@Controller
public class CoursePublishController {
    private final CoursePublishService coursePublishService;
    private String toVideo = "http://www.51xuecheng.cn/course/preview/learning.html?";
    public CoursePublishController(CoursePublishService coursePublishService) {
        this.coursePublishService = coursePublishService;
    }

    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable("courseId") Long courseId) {
        CoursePreviewDto coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        ModelAndView view = new ModelAndView();
        toVideo = toVideo + "id=" + coursePreviewInfo.getCourseBase().getId()
                + "&chapter" + coursePreviewInfo.getTeachplans().get(0).getId();
        coursePreviewInfo.setToVideo(toVideo);
        view.addObject("model", coursePreviewInfo);
        view.setViewName("course_template");
        return view;
    }

    @ResponseBody
    @PostMapping("/courseaudit/commit/{courseId}")
    public void commit(@PathVariable("courseId") Long courseId) {
        Long companyId = 1232141425L;
        coursePublishService.commitAudit(companyId, courseId);
    }

    @ResponseBody
    @PostMapping("/coursepublish/{courseId}")
    public void publish(@PathVariable("courseId") Long courseId) {
        Long companyId = 1232141425L;
        coursePublishService.publish(companyId, courseId);
    }

    @ResponseBody
    @GetMapping("/r/coursepublish/{courseId}")
    public CoursePublish getCoursePublish(@PathVariable("courseId") Long courseId) {
        return coursePublishService.getCoursePublish(courseId);
    }

    @ApiOperation("获取课程信息")
    @ResponseBody
    @GetMapping("/course/whole/{courseId}")
    public CoursePreviewDto getPublish(@PathVariable("courseId") Long courseId) {
        return coursePublishService.getPublish(courseId);
    }
}
